<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
  <head>
    <title>KRPC::Streaming::StreamsManager</title>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
    <link href='../../../css/style.css' media='screen' rel='stylesheet' type='text/css'>
    <script type='text/javascript'>
      //<![CDATA[
        function popupCode(url) {
          window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
        }
        
        function toggleCode(id) {
          var code = document.getElementById(id)
        
          code.style.display = code.style.display != 'block' ? 'block' : 'none'
          return true
        }
        
        // Make codeblocks hidden by default
        document.writeln('<' + 'style type="text/css">.method .source pre { display: none }<\/style>')
      //]]>
    </script>
  </head>
  <body class='page'>
    <div class='class' id='wrapper'>
      <div class='header'>
        <h1 class='name'>
          <span class='type'>class</span>
          KRPC::Streaming::StreamsManager
        </h1>
        <ol class='paths'>
          <li>
            <a target="docwin" href="../../../files/lib/krpc/streaming_rb.html">lib/krpc/streaming.rb</a>
          </li>
        </ol>
        <div class='parent'>
          Superclass:
          <strong><a target="docwin" href="../../Object.html">Object</a></strong>
        </div>
      </div>
      <div id='content'>
        <div id='text'>
          <div id='description'></div>
          <div id='method-list'>
            <h2>Methods</h2>
            <h3>Public Class</h3>
            <ol>
              <li><a target="docwin" href="#method-c-new">new</a></li>
            </ol>
            <h3>Public Instance</h3>
            <ol>
              <li><a target="docwin" href="#attribute-i-client">client</a></li>
              <li><a target="docwin" href="#method-i-create_stream">create_stream</a></li>
              <li><a target="docwin" href="#method-i-remove_all_streams">remove_all_streams</a></li>
              <li><a target="docwin" href="#method-i-remove_stream">remove_stream</a></li>
              <li><a target="docwin" href="#method-i-start_streaming_thread">start_streaming_thread</a></li>
              <li><a target="docwin" href="#method-i-stop_streaming_thread">stop_streaming_thread</a></li>
            </ol>
          </div>
          <div id='context'>
          </div>
          <div id='section'>
            <div id='attribute-list'>
              <h2 class='section-bar'>Attributes</h2>
              <div class='name-list'>
                <table>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-client'>client</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='methods'>
              <h2>Public Class methods</h2>
              <div class='method public-class' id='method-method-c-new'>
                <a name='method-c-new'></a>
                <div class='synopsis'>
                  <span class='name'>new</span>
                  <span class='arguments'>(client)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-new-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-new-source'><span class="ruby-comment"># File lib/krpc/streaming.rb, line 10</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">client</span>)&#x000A;  <span class="ruby-ivar">@client</span> = <span class="ruby-identifier">client</span>&#x000A;  <span class="ruby-ivar">@streams</span> = {}&#x000A;  <span class="ruby-ivar">@streams_mutex</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>&#x000A;  <span class="ruby-ivar">@streaming_thread</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> {}&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <h2>Public Instance methods</h2>
              <div class='method public-instance' id='method-method-i-create_stream'>
                <a name='method-i-create_stream'></a>
                <div class='synopsis'>
                  <span class='name'>create_stream</span>
                  <span class='arguments'>(request, return_type, method, *args, **kwargs)</span>
                </div>
                <div class='description'>
                  
                  <p>Send a streaming request, create related <a href="Stream.html">Stream</a>
                  object and return it. If identical <a href="Stream.html">Stream</a> already
                  exists, doesn&#39;t create new <a href="Stream.html">Stream</a> and return
                  the existing one.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-create_stream-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-create_stream-source'><span class="ruby-comment"># File lib/krpc/streaming.rb, line 19</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">create_stream</span>(<span class="ruby-identifier">request</span>, <span class="ruby-identifier">return_type</span>, <span class="ruby-identifier">method</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kwargs</span>)&#x000A;  <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>(<span class="ruby-string">&quot;Cannot stream a property setter&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">method</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">end_with?</span> <span class="ruby-string">&#39;=&#39;</span>&#x000A;  <span class="ruby-identifier">id</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">krpc</span>.<span class="ruby-identifier">add_stream</span>(<span class="ruby-identifier">request</span>)&#x000A;  <span class="ruby-ivar">@streams_mutex</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword">do</span>&#x000A;    <span class="ruby-keyword">if</span> <span class="ruby-ivar">@streams</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">id</span>&#x000A;      <span class="ruby-ivar">@streams</span>[<span class="ruby-identifier">id</span>]&#x000A;    <span class="ruby-keyword">else</span>&#x000A;      <span class="ruby-identifier">value</span> = <span class="ruby-identifier">method</span>.<span class="ruby-identifier">call</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kwargs</span>)&#x000A;      <span class="ruby-ivar">@streams</span>[<span class="ruby-identifier">id</span>] = <span class="ruby-constant">Stream</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">return_type</span>, <span class="ruby-identifier">value</span>, <span class="ruby-identifier">method</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kwargs</span>)&#x000A;    <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-remove_all_streams'>
                <a name='method-i-remove_all_streams'></a>
                <div class='synopsis'>
                  <span class='name'>remove_all_streams</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Remove all streams created by this streams manager.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-remove_all_streams-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-remove_all_streams-source'><span class="ruby-comment"># File lib/krpc/streaming.rb, line 47</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">remove_all_streams</span>&#x000A;  <span class="ruby-ivar">@streams</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">_</span>,<span class="ruby-identifier">stream</span><span class="ruby-operator">|</span> <span class="ruby-identifier">remove_stream</span>(<span class="ruby-identifier">stream</span>)}&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-remove_stream'>
                <a name='method-i-remove_stream'></a>
                <div class='synopsis'>
                  <span class='name'>remove_stream</span>
                  <span class='arguments'>(stream)</span>
                </div>
                <div class='description'>
                  
                  <p>Remove a streaming request and deactivate the <a
                  href="Stream.html">Stream</a> object. Returns <code>true</code> if
                  streaming request is removed or <code>false</code> if passed <a
                  href="Stream.html">Stream</a> object is already inactive.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-remove_stream-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-remove_stream-source'><span class="ruby-comment"># File lib/krpc/streaming.rb, line 34</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">remove_stream</span>(<span class="ruby-identifier">stream</span>)&#x000A;  <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">active?</span>&#x000A;  <span class="ruby-ivar">@streams_mutex</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword">do</span>&#x000A;    <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@streams</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">id</span>&#x000A;    <span class="ruby-identifier">client</span>.<span class="ruby-identifier">krpc</span>.<span class="ruby-identifier">remove_stream</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">id</span>&#x000A;    <span class="ruby-ivar">@streams</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">id</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">value</span> = <span class="ruby-constant">RuntimeError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;Stream has been removed&quot;</span>)&#x000A;  <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">mark_as_inactive</span>&#x000A;  <span class="ruby-keyword">true</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-start_streaming_thread'>
                <a name='method-i-start_streaming_thread'></a>
                <div class='synopsis'>
                  <span class='name'>start_streaming_thread</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Start streaming thread. It receives stream data, and updates <a
                  href="Stream.html">Stream</a> object&#39;s <code>value</code> attribute.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-start_streaming_thread-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-start_streaming_thread-source'><span class="ruby-comment"># File lib/krpc/streaming.rb, line 52</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">start_streaming_thread</span>&#x000A;  <span class="ruby-identifier">stop_streaming_thread</span>&#x000A;  <span class="ruby-ivar">@streaming_thread</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>&#x000A;    <span class="ruby-identifier">connection</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">stream_connection</span>&#x000A;    <span class="ruby-identifier">stream_message_type</span> = <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;KRPC.StreamMessage&quot;</span>]&#x000A;    <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>&#x000A;      <span class="ruby-identifier">size</span> = <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">recv_varint</span>&#x000A;      <span class="ruby-identifier">data</span> = <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-identifier">size</span>)&#x000A;      <span class="ruby-identifier">stream_msg</span> = <span class="ruby-constant">Decoder</span>.<span class="ruby-identifier">decode</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">stream_message_type</span>, <span class="ruby-identifier">client</span>)&#x000A;      <span class="ruby-ivar">@streams_mutex</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword">do</span>&#x000A;        <span class="ruby-identifier">stream_msg</span>.<span class="ruby-identifier">responses</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">stream_resp</span><span class="ruby-operator">|</span>&#x000A;          <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@streams</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">stream_resp</span>.<span class="ruby-identifier">id</span>&#x000A;          <span class="ruby-identifier">stream</span> = <span class="ruby-ivar">@streams</span>[<span class="ruby-identifier">stream_resp</span>.<span class="ruby-identifier">id</span>]&#x000A;          <span class="ruby-keyword">if</span> <span class="ruby-identifier">stream_resp</span>.<span class="ruby-identifier">response</span>.<span class="ruby-identifier">has_field?</span>(<span class="ruby-string">&quot;error&quot;</span>)&#x000A;            <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">value</span> = <span class="ruby-constant">RPCError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">stream_resp</span>.<span class="ruby-identifier">response</span>.<span class="ruby-identifier">error</span>)&#x000A;          <span class="ruby-keyword">else</span>&#x000A;            <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">value</span> = <span class="ruby-constant">Decoder</span>.<span class="ruby-identifier">decode</span>(<span class="ruby-identifier">stream_resp</span>.<span class="ruby-identifier">response</span>.<span class="ruby-identifier">return_value</span>, <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">return_type</span>, <span class="ruby-identifier">client</span>)&#x000A;          <span class="ruby-keyword">end</span>&#x000A;        <span class="ruby-keyword">end</span>&#x000A;      <span class="ruby-keyword">end</span>&#x000A;    <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-stop_streaming_thread'>
                <a name='method-i-stop_streaming_thread'></a>
                <div class='synopsis'>
                  <span class='name'>stop_streaming_thread</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Stop streaming thread.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-stop_streaming_thread-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-stop_streaming_thread-source'><span class="ruby-comment"># File lib/krpc/streaming.rb, line 77</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">stop_streaming_thread</span>&#x000A;  <span class="ruby-ivar">@streaming_thread</span>.<span class="ruby-identifier">terminate</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id='footer-push'></div>
    </div>
    <div id='footer'>
      <a target="docwin" href="http://github.com/mislav/hanna/tree/master"><strong>Hanna</strong> RDoc template</a>
    </div>
  </body>
</html>
